﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Tradex.Messages {

    /// <summary>
    /// Requests / informs of the creation of a LogEntry.
    /// </summary>
    [Serializable()]
    public class LogEntryCreateMessage : Message {
        
        /// <summary>
        /// Constructor taking all properties.
        /// </summary>
        /// <param name="timestamp"></param>
        /// <param name="traceLevel"></param>
        /// <param name="subject"></param>
        /// <param name="message"></param>
        /// <param name="origin"></param>
        /// <param name="reference"></param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
        public LogEntryCreateMessage (
            DateTime timestamp,
            TraceLevel traceLevel,

            string subject,
            string message,
            string origin = null,
            string reference = null
        ) {
            Timestamp = timestamp;

            TraceLevel = traceLevel;

            Subject = subject;
            Message = message;
            Origin = origin;
            Reference = reference;
        }

        /// <inheritdoc />
        public override MessageCode Code { get { return MessageCode.LogEntryCreate; } }

        /// <summary>
        /// This is the timestamp of the log entry. When submitting an entry it may be null, in which case
        /// it will be set at the control service (indicating the time the control service got the message).
        /// This normally will lead to another message, though, and should be seen as bad behavior.
        /// </summary>
        public DateTime Timestamp { get; private set; }

        /// <summary>
        /// This is the trace level of the log entry.
        /// </summary>
        public TraceLevel TraceLevel { get; private set; }

        /// <summary>
        /// This is a short string description of the log entry. This can be "Order Modification Failed", for example,
        /// with the Message then containing detailed error information.
        /// </summary>
        public string Subject { get; private set; }

        /// <summary>
        /// This is the detailed message for the log entry. IT will be more detailed than the subject.
        /// </summary>
        public string Message { get; private set; }

        /// <summary>
        /// This string indicates the origin of the message - this is the particular subsystem, connector or strategy that
        /// causes the log entry to be written.
        /// </summary>
        public string Origin { get; private set; }

        /// <summary>
        /// This string indicates a reference to the reason of the message. This can be the order that gets entered or blows,
        /// or the strategy that is started.
        /// </summary>
        public string Reference { get; private set; }

        /// <inheritdoc />
        public override string ToString() {
            //StringBuilder xinfo = new StringBuilder ();

            String result = String.Format(CultureInfo.InvariantCulture, "{0} {1} {2}", Timestamp, TraceLevel, Message);
            //if (xinfo.Length != 0) {
            //    result = result + " (" + xinfo.ToString () + ")";
            //}
            return result;
        }

    }
}
